<?php
	
	include("netronix.php");
	
	class MyDB extends SQLite3
	{
		function __construct()
		{
		$this->open('srcp/access.db');
		}
	}
	$db = new MyDB();
	if(!$db){
		echo $db->lastErrorMsg();
	} else {
		echo "Opened database successfully.\n";
	}
	/*
	$sql_table = "CREATE TABLE IF NOT EXISTS ACCESS (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, date TEXT NOT NULL)";
	
	$ret = $db->exec($sql_table);
	if(!$ret) {
		echo $db->lastErrorMsg();
	}
	*/
	
	$data = parse_ini_file('config.ini');
	
$fp = fsockopen($data["host"], $data["port"], $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)\n";
	} else {
		echo "Connected to serial port. \n";
		while (!feof($fp)) {
			// kiedy zblize karte wtedy z pliku stan czyta i wysyla do portu odpowiednie kombinacje zapisujace a nie zapisujesz do bazy uida
			$time = time() + 1; // fix jesli karta zostanie 2x szybko zblizona..
			$date = date('Y-m-d H:i:s', $time);
			$uid = rtrim(fgets($fp, 128));
			if(!empty($uid)) {
				$rfid_status = fopen("srcp/rfid_status", "r");
				$rfid_status_content = fread($rfid_status, 1);
					if($rfid_status_content == "1") { // odczyt danych z karty
						rfid_start:
						fwrite($fp, C_Login_User(array(0x31, 0x32, 0x33, 0x34, 0x00)));
						usleep(50*1000);
						fwrite($fp, C_SetAutoReaderConfig(array(0x00, 0x00, 0x01, 0x01, 0x00)));
						usleep(50*1000);
						fwrite($fp, C_LoadKeyToSKB(array(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00)));
						usleep(50*1000);
						fwrite($fp, C_TurnOnAntennaPower(array(0x01)));
						usleep(50*1000);
						fwrite($fp, C_Select(array(0x00)));
						usleep(50*1000);
						fwrite($fp, C_LoginWithSKB(array(0x02, 0xBB, 0x00)));
						usleep(50*1000);
						fwrite($fp, C_ReadBlock(array(0x00)));
						usleep(50*1000);
						fwrite($fp, C_ReadBlock(array(0x01)));
						usleep(50*1000);
						fwrite($fp, C_ReadBlock(array(0x02)));
						usleep(50*1000);
						//fwrite($fp, C_ReadBlock(array(0x01)));
						//usleep(50*1000);
						echo "Retrieving data from card.. (RFID_READ) \n";
						stream_set_timeout($fp, 2);
						//echo "Zapisuje do pliku..\n";
						$rfid_data = fgets($fp, 250);
						$rfid_data_fp = fopen("srcp/rfid_data", "w");
						fwrite($rfid_data_fp, $rfid_data);
						fclose($rfid_data_fp);
						// sprawdzamy poprawnosc odczytanych danych
						if( strpos(file_get_contents("srcp/rfid_data"),"%") == false) {
							echo "Error (no entry point found), retry..\n";
							goto rfid_start;
						}
						echo "Data from card successfully writed to file..\n";
						$rfid_status2 = fopen("srcp/rfid_status", "w");
						fwrite($rfid_status2, "0");
						fclose($rfid_status2);
						//sleep(1);
						usleep(50*1000);
						fwrite($fp, C_Login_User(array(0x31, 0x32, 0x33, 0x34, 0x00)));
						usleep(50*1000);
						fwrite($fp, C_SetAutoReaderConfig(array(0x01, 0x00, 0x01, 0x011, 0x01)));
						usleep(50*1000);
					} elseif($rfid_status_content == "3") { // nowy uzytkownik
						$uid_regexp = preg_replace('/[^A-Za-z0-9 _\-\+\&]/','',$uid);
						echo "[NEW_USER] New UID: ".$uid_regexp." added. \n";
						$rfid_data = fopen("srcp/rfid_data", "w");
						fwrite($rfid_data, $uid_regexp);
						fclose($rfid_data);
						$rfid_new_user = fopen("srcp/rfid_new_user", "r");
						$rfid_json_content = fread($rfid_new_user, 100);
						$rfid_data_json = json_decode($rfid_json_content, true);
						$name = $rfid_data_json['name'];
						$locker = $rfid_data_json['locker'];
						$coffee = $rfid_data_json['machine'];
						$name_char = unpack('C*',$name);
						$locker_char = unpack('C*',$locker);
						$coffee_char = unpack('C*',$coffee);
						for($i=0;$i<2;$i++) {
							fwrite($fp, C_Login_User(array(0x31, 0x32, 0x33, 0x34, 0x00)));
							usleep(50*1000);
							fwrite($fp, C_SetAutoReaderConfig(array(0x00, 0x00, 0x01, 0x01, 0x00)));
							usleep(50*1000);
							fwrite($fp, C_LoadKeyToSKB(array(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00)));
							usleep(50*1000);
							fwrite($fp, C_TurnOnAntennaPower(array(0x01)));
							usleep(50*1000);
							fwrite($fp, C_Select(array(0x00)));
							usleep(50*1000);
							fwrite($fp, C_LoginWithSKB(array(0x02, 0xBB, 0x00)));
							usleep(50*1000);
							@fwrite($fp, C_WriteBlock(array(0x00, 37, $name_char[1], $name_char[2], $name_char[3], $name_char[4], $name_char[5], $name_char[6], $name_char[7], $name_char[8], $name_char[9], $name_char[10], $name_char[11], $name_char[12], $name_char[13], $name_char[14], 37)));
							usleep(50*1000);
							@fwrite($fp, C_WriteBlock(array(0x01, 37, $locker_char[1], $locker_char[2], $locker_char[3], $locker_char[4], 37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)));
							usleep(50*1000);
							@fwrite($fp, C_WriteBlock(array(0x02, 37, $coffee_char[1], $coffee_char[2], $coffee_char[3], $coffee_char[4], 37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)));
							usleep(50*1000);
						}
						echo "[NEW_USER] New name saved on card. \n";
						$rfid_status2 = fopen("srcp/rfid_status", "w");
						fwrite($rfid_status2, "0");
						//sleep(1);
						usleep(50*1000);
						fwrite($fp, C_Login_User(array(0x31, 0x32, 0x33, 0x34, 0x00)));
						usleep(50*1000);
						fwrite($fp, C_SetAutoReaderConfig(array(0x01, 0x00, 0x01, 0x011, 0x01)));
						usleep(50*1000);
					}
					elseif($rfid_status_content == "2") { // zapis danych na karte
						echo "Writing data to the card.. (RFID_SAVE) \n";
						$rfid_data = fopen("srcp/rfid_data", "r");
						$rfid_json_content = fread($rfid_data, 100);
						$rfid_data_json = json_decode($rfid_json_content, true);
						//rfid_save_start:
						$name = $rfid_data_json['name'];
						$locker = $rfid_data_json['locker'];
						$coffee = $rfid_data_json['machine'];
						$name_char = unpack('C*',$name);
						$locker_char = unpack('C*',$locker);
						$coffee_char = unpack('C*',$coffee);
						for($i=0;$i<2;$i++) {
							fwrite($fp, C_Login_User(array(0x31, 0x32, 0x33, 0x34, 0x00)));
							usleep(50*1000);
							fwrite($fp, C_SetAutoReaderConfig(array(0x00, 0x00, 0x01, 0x01, 0x00)));
							usleep(50*1000);
							fwrite($fp, C_LoadKeyToSKB(array(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00)));
							usleep(50*1000);
							fwrite($fp, C_TurnOnAntennaPower(array(0x01)));
							usleep(50*1000);
							fwrite($fp, C_Select(array(0x00)));
							usleep(50*1000);
							fwrite($fp, C_LoginWithSKB(array(0x02, 0xBB, 0x00)));
							usleep(50*1000);
							@fwrite($fp, C_WriteBlock(array(0x00, 37, $name_char[1], $name_char[2], $name_char[3], $name_char[4], $name_char[5], $name_char[6], $name_char[7], $name_char[8], $name_char[9], $name_char[10], $name_char[11], $name_char[12], $name_char[13], $name_char[14], 37)));
							usleep(50*1000);
							@fwrite($fp, C_WriteBlock(array(0x01, 37, $locker_char[1], $locker_char[2], $locker_char[3], $locker_char[4], 37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)));
							usleep(50*1000);
							@fwrite($fp, C_WriteBlock(array(0x02, 37, $coffee_char[1], $coffee_char[2], $coffee_char[3], $coffee_char[4], 37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)));
							usleep(50*1000);
						}
						$rfid_status2 = fopen("srcp/rfid_status", "w");
						fwrite($rfid_status2, "0");
						//sleep(1);
						usleep(50*1000);
						fwrite($fp, C_Login_User(array(0x31, 0x32, 0x33, 0x34, 0x00)));
						usleep(50*1000);
						fwrite($fp, C_SetAutoReaderConfig(array(0x01, 0x00, 0x01, 0x011, 0x01)));
						usleep(50*1000);
						//stream_set_timeout($fp, 1);
						//$rfid_data = fgets($fp, 55);
						//$hex = unpack('C*',$rfid_data);
						//$hex_string = implode($hex, "");
						//echo "response: ".$hex_string;
						//echo "\n\n";
						//if (strpos($hex_string,'16179255145186168925511019516232556401617255234166') !== false) {
						echo "New data stored on the card successfully.. \n";
						//} else {
						//	goto rfid_save_start;
						//}
						$rfid_status2 = fopen("srcp/rfid_status", "w");
						fwrite($rfid_status2, "0");
						sleep(1);
						fwrite($fp, C_Login_User(array(0x31, 0x32, 0x33, 0x34, 0x00)));
						usleep(50*1000);
						fwrite($fp, C_SetAutoReaderConfig(array(0x01, 0x00, 0x01, 0x011, 0x01)));
					} else {
						$sql = "INSERT INTO ACCESS_".$uid." (date) VALUES ('$date')";
						@$ret = $db->exec($sql);
						if(!$ret) {
							//echo $db->lastErrorMsg()."\n";
						} else {
							echo "New UID: ".$uid." added. \n";
						}
					}
				}
			}
	fclose($fp);
}

$db->close();

?>